package com.xingfudazi.web.controller.app;

import com.xingfudazi.app.domain.AppRoleApply;
import com.xingfudazi.app.enums.ApplyStatusEnum;
import com.xingfudazi.app.enums.MemberTypeEnum;
import com.xingfudazi.app.service.IAppRoleApplyService;
import com.xingfudazi.common.core.controller.BaseController;
import com.xingfudazi.common.core.domain.AjaxResult;
import com.xingfudazi.common.core.domain.Result;
import com.xingfudazi.common.core.page.TableDataInfo;
import com.xingfudazi.common.enums.BusinessType;
import com.xingfudazi.common.utils.StringUtils;
import com.xingfudazi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author YuJiaMing
 * date 2024年06月22日 22:05
 */
@RestController
@RequestMapping("/app/role-apply")
public class AppRoleApplyController extends BaseController {

    @Autowired
    private IAppRoleApplyService appRoleApplyService;

    @PostMapping("add")
    public Result<?> add(@RequestBody AppRoleApply appRoleApply) {
        String type = appRoleApply.getType();
        if (StringUtils.isBlank(type) || StringUtils.isBlank(appRoleApply.getIntroduce())) {
            throw new IllegalArgumentException();
        }

        MemberTypeEnum memberTypeEnum = MemberTypeEnum.valueOf(type);
        if (memberTypeEnum == MemberTypeEnum.MEMBER) {
            throw new IllegalArgumentException();
        }

        appRoleApplyService.add(appRoleApply);
        return Result.success();
    }


    /**
     * 查询动态列表
     */
    @GetMapping("/list")
    public TableDataInfo<AppRoleApply> list(AppRoleApply appRoleApply) {
        startPage();
        List<AppRoleApply> list = appRoleApplyService.list(appRoleApply);
        return getDataTable(list);
    }


    /**
     * 审批
     */
    @PostMapping("process")
    public Result<?> process(@RequestBody AppRoleApply appRoleApply) {
        String status = appRoleApply.getStatus();
        Long id = appRoleApply.getId();
        if (StringUtils.isBlank(status) || id == null){
            throw new IllegalArgumentException();
        }

        this.appRoleApplyService.process(id, ApplyStatusEnum.valueOf(status));
        return Result.success();
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppRoleApply appRoleApply)
    {
        List<AppRoleApply> list = appRoleApplyService.selectAppRoleApplyList(appRoleApply);
        ExcelUtil<AppRoleApply> util = new ExcelUtil<AppRoleApply>(AppRoleApply.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(appRoleApplyService.selectAppRoleApplyById(id));
    }


    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:apply:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody AppRoleApply appRoleApply)
    {
        return toAjax(appRoleApplyService.updateAppRoleApply(appRoleApply));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:apply:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appRoleApplyService.deleteAppRoleApplyByIds(ids));
    }


}
